import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RegistersignPage } from '../registersign/registersign';

import { HttpserviesProvider } from '../../providers/httpservies/httpservies';
import { Http } from '@angular/http';
import { StorageProvider } from '../../providers/storage/storage';


/**
 * Generated class for the RegisterPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage {

  public tel = '';  //电话号码

  constructor(public navCtrl: NavController, public navParams: NavParams, public httpServe:HttpserviesProvider,public storage:StorageProvider) {
    
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegisterPage');
  }

  //页面跳转
  goRegisterSign(){
    // this.navCtrl.push(RegistersignPage);

    console.log(this.tel);

    //1.验证手机号码是否合法， 利用正则法则判断
    //2.发送post请求获取验证码
    if(/^\d{11}$/.test(this.tel)){
      var url = 'api/sendCode';
      this.httpServe.doPostData(url,{"tel":this.tel},(data)=>{
        console.log(data);
        if(data.success){//获取到验证码成功
          //保存电话号码
          this.storage.set('register_tel',this.tel);
          //跳转下个页面，验证验证码
          this.navCtrl.push(RegistersignPage);
        }else{
          alert('获取验证码失败，请稍后重试');
        }
      })
    }else{
      alert('请输入正确的手机号码');
    }
  }

}
